#!/bin/bash
set -u

launcher_root="$(cd "$(dirname "$0")" && pwd)"
payload_root="$(cd "$launcher_root/../Resources/payload" && pwd)"
helper_root="$HOME/Library/Application Support/NotebookLM Batch Studio Helper"
mkdir -p "$helper_root/runtime"
log_path="$helper_root/runtime/install.log"

/usr/bin/osascript -e 'display notification "第一次安裝可能需要數分鐘，完成後會自動開啟網站。" with title "NotebookLM Batch Studio"'
install_helper() {
  /usr/bin/ditto "$payload_root" "$helper_root"
  "$helper_root/scripts/install-macos.sh"
}

if install_helper >"$log_path" 2>&1; then
  /usr/bin/osascript -e 'display dialog "NotebookLM 助手已在背景啟動。往後登入 Mac 時會自動啟動，不需要保持終端機視窗。" buttons {"好"} default button "好" with title "安裝完成"'
else
  /usr/bin/osascript -e 'display dialog "安裝未完成。安裝紀錄位於「資源庫/Application Support/NotebookLM Batch Studio Helper/runtime/install.log」。" buttons {"好"} default button "好" with icon stop with title "安裝失敗"'
  exit 1
fi
